﻿using Chloe.SqlServer;
using Infrastructure.Web;
using MyData.DB;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
  public  class CarRecordApp
    {
        private ICarRecordRepository _carRecordRepository;
        public CarRecordApp(ICarRecordRepository carRecordRepository)
        {
            _carRecordRepository = carRecordRepository;
        }
        public PageDto<CarRecordDto> GetCarRecord(QueryListModel query, string carPlant, DateTime? start, DateTime? end)
        {
            using (var db = new MsSqlContext(DbHelper.ConnectionString))
            {
                var linq = db.Query<CarRecord>().Select(c=>new CarRecordDto() {
                    Id=c.Id,
                    CarPlant=c.CarPlant,
                    Company=c.Company,
                    CreatedTime=c.CreatedTime,
                    InTime=c.InTime,
                    OutTime=c.OutTime,
                    Reason=c.Reason,
                    UserName=c.UserName,
                    PersonNum=c.PersonNum
                   
                });
            
                if (!string.IsNullOrEmpty(carPlant))
                {
                    linq = linq.Where(c=>c.CarPlant.Contains(carPlant)||c.Company.Contains(carPlant));
                }
                if (start.HasValue)
                {
                    linq = linq.Where(c => c.CreatedTime >=start.Value);
                }
                if (end.HasValue)
                {
                    linq = linq.Where(c => c.CreatedTime <= end.Value);
                }
                WebHelper.WriteSession("Data", linq.ToList());
                var result = linq.ToList().AsQueryable().Paging(query.Limit, query.Offset);
                return new PageDto<CarRecordDto>()
                {
                    total = result.total,
                    rows = result.rows
                };
            }
        }
    }
}
